package com.antaohua.platfrom.framework.base.utils;

import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaBuilder.In;
import javax.persistence.criteria.Predicate;
import javax.persistence.criteria.Root;

import org.hibernate.criterion.Criterion;
import org.hibernate.criterion.MatchMode;
import org.hibernate.criterion.Restrictions;

public enum Condition {

	/**
	 * 等于，=
	 * */
	eq{
		@Override
		public Criterion getHibernateCriterion(String propertyName,Object value) {
			return Restrictions.eq(propertyName,value);
		}
		public <T> Predicate getJpaCriterion(CriteriaBuilder criteriaBuilder,Root<T> entityRoot ,String propertyName,Object value) {
			return criteriaBuilder.equal(entityRoot.get(propertyName),value);
		}
	},
	/**
	 * 等于或者空
	 * */
	eqOrIsNull{
		@Override
		public Criterion getHibernateCriterion(String propertyName,Object value) {
			return Restrictions.eqOrIsNull(propertyName,value);
		}
		public <T> Predicate getJpaCriterion(CriteriaBuilder criteriaBuilder,Root<T> entityRoot ,String propertyName,Object value) {
			if(value==null) {
				return criteriaBuilder.isNull(entityRoot.get(propertyName));
			}else {
				return criteriaBuilder.equal(entityRoot.get(propertyName),value);
			}
		}
	},
	/**
	 * 不等于
	 * */
	ne{
		@Override
		public Criterion getHibernateCriterion(String propertyName,Object value) {
			return Restrictions.ne(propertyName,value);
		}
		public <T> Predicate getJpaCriterion(CriteriaBuilder criteriaBuilder,Root<T> entityRoot ,String propertyName,Object value) {
			return criteriaBuilder.not(entityRoot.get(propertyName));
		}
	},
	/**
	 * 不等于或者不等于空
	 * */
	neOrIsNotNull{
		@Override
		public Criterion getHibernateCriterion(String propertyName,Object value) {
			return Restrictions.neOrIsNotNull(propertyName,value);
		}
		public <T> Predicate getJpaCriterion(CriteriaBuilder criteriaBuilder,Root<T> entityRoot ,String propertyName,Object value) {
			if(value==null) {
				return criteriaBuilder.isNotNull(entityRoot.get(propertyName));
			}else {
				return criteriaBuilder.notEqual(entityRoot.get(propertyName),value);
			}
			
		}
	},
	/**
	 * like
	 * */
	like{
		@Override
		public Criterion getHibernateCriterion(String propertyName,Object value) {
			return Restrictions.like(propertyName,MatchMode.ANYWHERE.toMatchString(value.toString()));
		}
		public <T> Predicate getJpaCriterion(CriteriaBuilder criteriaBuilder,Root<T> entityRoot ,String propertyName,Object value) {
			return criteriaBuilder.like(entityRoot.get(propertyName), value.toString());
		}
	},
	/**
	 * 小于，>
	 * */
	gt{
		@Override
		public Criterion getHibernateCriterion(String propertyName,Object value) {
			return Restrictions.gt(propertyName,value);
		}
		public <T> Predicate getJpaCriterion(CriteriaBuilder criteriaBuilder,Root<T> entityRoot ,String propertyName,Object value) {
			return criteriaBuilder.gt(entityRoot.get(propertyName),(Number)value);
		}
	},
	/**
	 * 大于，<
	 * */
	lt{
		@Override
		public Criterion getHibernateCriterion(String propertyName,Object value) {
			return Restrictions.lt(propertyName,value);
		}
		public <T> Predicate getJpaCriterion(CriteriaBuilder criteriaBuilder,Root<T> entityRoot ,String propertyName,Object value) {
			return criteriaBuilder.lt(entityRoot.get(propertyName),(Number)value);
		}
	},
	/**
	 * 大于等于，<=
	 * */
	le{
		@Override
		public Criterion getHibernateCriterion(String propertyName,Object value) {
			return Restrictions.le(propertyName,value);
		}
		public <T> Predicate getJpaCriterion(CriteriaBuilder criteriaBuilder,Root<T> entityRoot ,String propertyName,Object value) {
			return criteriaBuilder.le(entityRoot.get(propertyName),(Number)value);
		}
	},
	/**
	 * 小于等于，>=
	 * */
	ge{
		@Override
		public Criterion getHibernateCriterion(String propertyName,Object value) {
			return Restrictions.ge(propertyName,value);
		}
		public <T> Predicate getJpaCriterion(CriteriaBuilder criteriaBuilder,Root<T> entityRoot ,String propertyName,Object value) {
			return criteriaBuilder.ge(entityRoot.get(propertyName),(Number)value);
		}
	},
	/**
	 * in
	 * */
	in{
		@Override
		public Criterion getHibernateCriterion(String propertyName,Object value) {
			return Restrictions.in(propertyName,value);
		}
		public <T> Predicate getJpaCriterion(CriteriaBuilder criteriaBuilder,Root<T> entityRoot ,String propertyName,Object value) {
			In<T> in = criteriaBuilder.in(entityRoot.get(propertyName));
			in.in(value);
			return in;
		}
	},
	/**
	 * 等于空
	 * */
	isNull{
		@Override
		public Criterion getHibernateCriterion(String propertyName,Object value) {
			return Restrictions.isNull(propertyName);
		}
		public <T> Predicate getJpaCriterion(CriteriaBuilder criteriaBuilder,Root<T> entityRoot ,String propertyName,Object value) {
			return criteriaBuilder.isNull(entityRoot.get(propertyName));
		}
	},
	/**
	 * 不等于空
	 * */
	isNotNull{
		@Override
		public Criterion getHibernateCriterion(String propertyName,Object value) {
			return Restrictions.isNotNull(propertyName);
		}
		@Override
		public <T> Predicate getJpaCriterion(CriteriaBuilder criteriaBuilder,Root<T> entityRoot ,String propertyName,Object value) {
			return criteriaBuilder.isNotNull(entityRoot.get(propertyName));
		}
	};
	
	public abstract Criterion getHibernateCriterion(String propertyName,Object value);
	
	public abstract <T> Predicate getJpaCriterion(CriteriaBuilder criteriaBuilder,Root<T> entityRoot ,String propertyName,Object value);
	
}
